package com.wolan.utils.interceptor;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by fei.chen on 2016/3/21.
 */
public class UserAuthenInterceptor implements HandlerInterceptor {
    protected Logger logger = Logger.getLogger(getClass());
    private List<String> excludedUrls;
   /* @Autowired
    private BusinessExceptionFactory exceptionFactory;*/

    public List<String> getExcludedUrls() {
        return excludedUrls;
    }
    public void setExcludedUrls(List<String> excludedUrls) {
        this.excludedUrls = excludedUrls;
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.info("***********UserAuthenInterceptor************");
        //除了注册/登录之外都要拦截
        //注册/登录 返回 true
        String requestUri = httpServletRequest.getRequestURI();
        System.out.println("requestUri = "+requestUri);
        for (String url : excludedUrls) {
            System.out.println("excludedUrl = "+url);
            if (requestUri.endsWith(url)) {
                return true;
            }
        }

        //拦截到，先获取header 字段
        //判断signid 是否为空 false
        //signid 错误 false
        //signid 过期 false
       /* String signId = httpServletRequest.getHeader("signId");
        //sign 为空
        if(signId == null)
        {
            // 处理 response
            responseOutWithJson(httpServletResponse,exceptionFactory.createBusinessException("10014"));
            return false;
        }*/
        /**
         User user = userService.findByPhoneNumber(Authutil.getPhoneNumber(signId));
         //用户为空
         if(user==null)
         {
         // 处理 response
         responseOutWithJson(httpServletResponse,exceptionFactory.createBusinessException("10014"));
         return false;
         }
         //sign 不匹配
         if(!user.getSignId().trim().equals(signId)){
         // 处理 response
         responseOutWithJson(httpServletResponse,exceptionFactory.createBusinessException("10014"));
         return false;
         }*/
       /* if(!Authutil.authenMd5(signId)){

            responseOutWithJson(httpServletResponse,exceptionFactory.createBusinessException("10014"));
        }
        //sign 过期
        if(Authutil.isEXP(signId) ){
            // 处理 response
            responseOutWithJson(httpServletResponse,exceptionFactory.createBusinessException("10014"));
            return false;
        }*/

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
    /**
     * 以JSON格式输出
     * @param response
     */
    /*protected void responseOutWithJson(HttpServletResponse response,
                                       BusinessException responseObject){

        ResponseEntity<Void> result = new ResponseEntity<>();
        result.setErrorCode(responseObject.getErrorCode());
        result.setMessage(responseObject.getMessage());
        //将实体对象转换为JSON Object转换
        JSONObject responseJSONObject = JSONObject.fromObject(result);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(responseJSONObject.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }*/
}
